home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / fileio.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  3KB  |  147 lines

  1. /*
  2.  * The routines in this file
  3.  * read and write ASCII files from the
  4.  * disk. All of the knowledge about files
  5.  * are here. A better message writing
  6.  * scheme should be used.
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. FILE    *ffp;                   /* File pointer, all functions. */
  12.  
  13. /*
  14.  * Open a file for reading.
  15.  */
  16. ffropen(fn)
  17. char    *fn;
  18. {
  19.         if ((ffp=fopen(fn, "r")) == NULL)
  20.                 return (FIOFNF);
  21.         return (FIOSUC);
  22. }
  23.  
  24. /*
  25.  * Open a file for writing.
  26.  * Return TRUE if all is well, and
  27.  * FALSE on error (cannot create).
  28.  */
  29. ffwopen(fn)
  30. char    *fn;
  31. {
  32. #if     VMS
  33.         register int    fd;
  34.  
  35.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  36.         || (ffp=fdopen(fd, "w")) == NULL) {
  37. #else
  38.         if ((ffp=fopen(fn, "w")) == NULL) {
  39. #endif
  40.                 mlwrite("Cannot open file for writing");
  41.                 return (FIOERR);
  42.         }
  43.         return (FIOSUC);
  44. }
  45.  
  46. /*
  47.  * Close a file.
  48.  * Should look at the status in all systems.
  49.  */
  50. ffclose()
  51. {
  52. #if     ALCYON
  53.         if (fclose(ffp) != FALSE) {
  54.                 mlwrite("Error closing file");
  55.                 return(FIOERR);
  56.         }
  57.         return(FIOSUC);
  58. #endif
  59.         fclose(ffp);
  60.         return (FIOSUC);
  61. }
  62.  
  63. /*
  64.  * Write a line to the already
  65.  * opened file. The "buf" points to the
  66.  * buffer, and the "nbuf" is its length, less
  67.  * the free newline. Return the status.
  68.  * Check only at the newline.
  69.  */
  70. ffputline(buf, nbuf)
  71. register char   buf[];
  72. register int nbuf;
  73. {
  74.         register int    i;
  75.  
  76.         for (i=0; i<nbuf; ++i)
  77.                 putc(buf[i]&0xFF, ffp);
  78.         putc('\n', ffp);
  79.         if (ferror(ffp) != FALSE) {
  80.                 mlwrite("Write I/O error");
  81.                 return (FIOERR);
  82.         }
  83.         return (FIOSUC);
  84. }
  85.  
  86. /*
  87.  * Read a line from a file,
  88.  * and store the bytes in the supplied
  89.  * buffer. The "nbuf" is the length of the
  90.  * buffer. Complain about long lines and lines
  91.  * at the end of the file that don't have a
  92.  * newline present. Check for I/O errors
  93.  * too. Return status.
  94.  */
  95. ffgetline(buf, nbuf)
  96. register char   buf[];
  97. register int nbuf;
  98. {
  99.         register int    c;
  100.         register int    i;
  101.  
  102.         i = 0;
  103.         while ((c=getc(ffp))!=EOF && c!='\n') {
  104.                 if (i >= nbuf-1) {
  105.                         mlwrite("File has long line");
  106.                         return (FIOERR);
  107.                 }
  108.                 buf[i++] = c;
  109.         }
  110.         if (c == EOF) {
  111.                 if (ferror(ffp) != FALSE) {
  112.                         mlwrite("File read error");
  113.                         return (FIOERR);
  114.                 }
  115.                 if (i != 0) {
  116.                         mlwrite("File has funny line at EOF");
  117.                         return (FIOERR);
  118.                 }
  119.                 return (FIOEOF);
  120.         }
  121.         buf[i] = 0;
  122.         return (FIOSUC);
  123. }
  124.  
  125. /* Function to handle printing file pageheaders.  Needs access to ffp,
  126.  * so it's here rather than in print.c where it probably belongs.  Two
  127.  * lines of top margin and three lines between header and text are wired in.
  128.  */
  129.  
  130. prhdg(f, pagenum, rm)
  131. int f;
  132. int pagenum;
  133. int rm;
  134. {
  135.         fflush(ffp);
  136.         if (f == TRUE)
  137.                 fprintf(ffp, "\f\n\n%*s\n%*s\n%*d\n\n\n",
  138.                         rm, &prnhdr[0], rm, &prndate[0],
  139.                         rm, pagenum);
  140.         else
  141.                 fprintf(ffp, "\n\n%*s\n%*s\n\n\n\n",
  142.                         rm, &prnhdr[0], rm, &prndate[0]);
  143.  
  144.         return(TRUE);
  145. }
  146.  
  147.